<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                   http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.15.xsd">
    <changeSet id="create-table-MATCHES-1" author="alisitsin (generated)">
        <preConditions onFail="MARK_RAN">
            <not>
                <tableExists tableName="matches"/>
            </not>
        </preConditions>
        <createTable tableName="matches">
            <column name="match_id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="pk_matches"/>
            </column>
            <column name="stage_id" type="BIGINT">
                <constraints foreignKeyName="FK_MATCHES_ON_STAGE" referencedTableName="match_stages"
                             referencedColumnNames="match_stage_id" nullable="true"/>
            </column>
            <column name="result" type="VARCHAR(255)"/>
        </createTable>
        <comment>create table MATCHES</comment>
    </changeSet>

    <changeSet id="create-table-match_players-1" author="alisitsin (generated)">
        <preConditions onFail="MARK_RAN">
            <not>
                <tableExists tableName="match_players"/>
            </not>
        </preConditions>
        <createTable tableName="match_players">
            <column name="match_id" type="BIGINT">
                <constraints foreignKeyName="fk_match_players_on_match"
                             referencedTableName="matches" referencedColumnNames="match_id" nullable="false"/>
            </column>
            <column name="players_ids" type="BIGINT"/>
        </createTable>
    </changeSet>

    <changeSet id="add-match_id_seq-1" author="alisitsin (generated)">
        <preConditions onFail="MARK_RAN">
            <not>
                <sequenceExists sequenceName="match_id_seq"/>
            </not>
        </preConditions>
        <createSequence incrementBy="1" sequenceName="match_id_seq" startValue="1000" minValue="1000"/>
        <comment>add match_id_seq</comment>
    </changeSet>

    <changeSet id="add-column-is_finished and is_started-1" author="alexander (generated)">
        <preConditions onFail="MARK_RAN">
            <and>
                <tableExists tableName="matches"/>
                <not>
                    <columnExists tableName="matches" columnName="is_finished"/>
                </not>
            </and>
        </preConditions>
        <addColumn tableName="matches">
            <column name="is_finished" type="BOOLEAN"/>
            <column name="is_started" type="BOOLEAN"/>
        </addColumn>
        <comment>add column is_finished and is_started</comment>
    </changeSet>

    <changeSet id="1702026171087-5"
               author="alexander (generated)">
        <addUniqueConstraint columnNames="stage_id"
                             constraintName="uc_matches_stage"
                             tableName="matches"/>
    </changeSet>
    <changeSet id="change-column-type-players_ids-1" author="alexander (generated)">
        <preConditions onFail="MARK_RAN">
            <and>
                <tableExists tableName="match_players"/>
                <columnExists tableName="match_players" columnName="players_ids"/>
            </and>
        </preConditions>
        <dropColumn columnName="players_ids" tableName="match_players"/>
        <addColumn tableName="match_players">
            <column name="players_ids" type="VARCHAR(4096)"/>
        </addColumn>
        <comment>change column type players_ids</comment>
    </changeSet>

    <changeSet id="add-column-winner-1" author="alexander (generated)">
        <preConditions onFail="MARK_RAN">
            <and>
                <tableExists tableName="matches"/>
                <not>
                    <columnExists tableName="matches" columnName="winner"/>
                </not>
            </and>
        </preConditions>
        <addColumn tableName="matches">
            <column name="winner" type="VARCHAR(4096)"/>
        </addColumn>
        <comment>add column winner</comment>
    </changeSet>

    <changeSet id="create-table-match_winners-1" author="alexander (generated)">
        <preConditions onFail="MARK_RAN">
            <not>
                <tableExists tableName="match_winners"/>
            </not>
        </preConditions>
        <createTable tableName="match_winners">
            <column name="match_id" type="BIGINT">
                <constraints nullable="false"
                             foreignKeyName="fk_match_winners_on_match"
                             referencedTableName="matches" referencedColumnNames="match_id"/>
            </column>
            <column name="winner_ids" type="VARCHAR(4096)"/>
        </createTable>
    </changeSet>
    <changeSet id="drop-column-winner-1" author="alexander (generated)">
        <preConditions>
            <and>
                <tableExists tableName="matches"/>
                <columnExists tableName="matches" columnName="winner"/>
            </and>
        </preConditions>
        <dropColumn columnName="winner" tableName="matches"/>
    </changeSet>

</databaseChangeLog>